循环中java数组的使用
counts[d1 + d2] += 1
真正的作用是什么?这是我的代码:
//call simulate(10)
public static void simulate(int rolls) {
Random rand = new Random();
int[] counts = new int[13];
for (int k = 0; k < rolls; k++) {
int d1 = rand.nextInt(6) + 1;
int d2 = rand.nextInt(6) + 1;
System.out.println(d1+"+"+d2+"+"+"="+(d1+d2));
counts[d1 + d2] += 1;
}
for (int k = 2; k <= 12; k++) {
System.out.println(k + "'s=\t" + counts[k] + "\t" + 100.0 * counts[k]/rolls);
}
}
共 (0) 个答案